package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.EmergencyTypeMapper;
import com.ruoyi.system.domain.EmergencyType;
import com.ruoyi.system.service.IEmergencyTypeService;

/**
 * 紧急事件类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-30
 */
@Service
public class EmergencyTypeServiceImpl implements IEmergencyTypeService 
{
    @Autowired
    private EmergencyTypeMapper emergencyTypeMapper;

    /**
     * 查询紧急事件类型
     * 
     * @param etId 紧急事件类型ID
     * @return 紧急事件类型
     */
    @Override
    public EmergencyType selectEmergencyTypeById(Long etId)
    {
        EmergencyType emergency = emergencyTypeMapper.selectEmergencyTypeById(etId);
        EmergencyType emergencyType = new EmergencyType();
        emergencyType.setEtParent(etId);
        emergency.setEmergencyTypeList(emergencyTypeMapper.selectEmergencyTypeList(emergencyType));
        return emergency;
    }

    /**
     * 查询紧急事件类型列表
     * 
     * @param emergencyType 紧急事件类型
     * @return 紧急事件类型
     */
    @Override
    public List<EmergencyType> selectEmergencyTypeList(EmergencyType emergencyType)
    {
        emergencyType.setEtParent(-1L);
        List<EmergencyType> emergencyTypes = emergencyTypeMapper.selectEmergencyTypeList(emergencyType);
        for (int i = 0; i <emergencyTypes.size(); i++) {
            emergencyType.setEtParent(emergencyTypes.get(i).getEtId());
            emergencyTypes.get(i).setEmergencyTypeList(emergencyTypeMapper.selectEmergencyTypeList(emergencyType));
        }
        return emergencyTypes;
    }

    /**
     * 新增紧急事件类型
     * 
     * @param emergencyType 紧急事件类型
     * @return 结果
     */
    @Override
    public int insertEmergencyType(EmergencyType emergencyType)
    {   emergencyType.setCreateTime(DateUtils.getNowDate());
        return emergencyTypeMapper.insertEmergencyType(emergencyType);
    }

    /**
     * 修改紧急事件类型
     * 
     * @param emergencyType 紧急事件类型
     * @return 结果
     */
    @Override
    public int updateEmergencyType(EmergencyType emergencyType)
    { emergencyType.setUpdateTime(DateUtils.getNowDate());
        return emergencyTypeMapper.updateEmergencyType(emergencyType);
    }

    /**
     * 批量删除紧急事件类型
     * 
     * @param etIds 需要删除的紧急事件类型ID
     * @return 结果
     */
    @Override
    public int deleteEmergencyTypeByIds(Long[] etIds)
    {
        for (int i = 0; i <etIds.length ; i++) {
            EmergencyType emergencyType=new EmergencyType();
            emergencyType.setEtParent(etIds[i]);
            List<EmergencyType> emergencyTypes = emergencyTypeMapper.selectEmergencyTypeList(emergencyType);
            if(emergencyTypes.size()>0){
                return 0;
            }else {
                return emergencyTypeMapper.deleteEmergencyTypeByIds(etIds);
            }
        }
        return 0;
    }

    /**
     * 删除紧急事件类型信息
     * 
     * @param etId 紧急事件类型ID
     * @return 结果
     */
    @Override
    public int deleteEmergencyTypeById(Long etId)
    {
        return emergencyTypeMapper.deleteEmergencyTypeById(etId);
    }
}
